package com.huiyujiaoyu.util;


import android.content.Context;
import android.content.res.AssetManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.webkit.WebView;
import android.widget.LinearLayout;

import com.huiyujiaoyu.base.AppContext;
import com.huiyujiaoyu.powertime.R;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import static android.R.attr.data;
import static com.huiyujiaoyu.http.HttpApi.OTHERBASE_URL;
import static com.huiyujiaoyu.util.Constant.ZOOM_CONTROLS_TIMEOUT;

/**
 * Created by yujunlong on 2017/7/20.
 */

public class StringUtils {
    public static String getFullUrl(String url) {
        if (TextUtils.isEmpty(url)) {
            return "";
        } else {
            return url.contains("http://") || url.contains("https://") ? url : OTHERBASE_URL + url;
        }
    }


    public static int getResId(int status){
        int resid = 0;
        switch (status) {
            case 0:
                resid = R.mipmap.icon_discovery_mianfei;
                break;
            case 1:
                resid = R.mipmap.icon_discovery_xianmian;
                break;
            case 2:
                break;
        }
        return resid;

    }
    public static int getResId2(int status){
        int resid = 1;
        switch (status) {
            case Constant.CONTENT_TYPE_YP:
                resid = R.mipmap.icon_yinpinbofangcishu;
                break;
            case Constant.CONTENT_TYPE_SP:
                resid = R.mipmap.icon_shipinbofangcishu;
                break;
            case Constant.CONTENT_TYPE_WZ:
                resid = R.mipmap.icon_yuedushu;
                break;
        }
        return resid;

    }

    public static void setVisibility(View itemView,boolean isVisible){
        RecyclerView.LayoutParams param = (RecyclerView.LayoutParams)itemView.getLayoutParams();
        if (isVisible){
            param.height = LinearLayout.LayoutParams.WRAP_CONTENT;
            param.width = LinearLayout.LayoutParams.MATCH_PARENT;
            itemView.setVisibility(View.VISIBLE);
        }else{
            itemView.setVisibility(View.GONE);
            param.height = 0;
            param.width = 0;
        }
        itemView.setLayoutParams(param);
    }

    public static String getJson(Context context, String fileName) {

        StringBuilder stringBuilder = new StringBuilder();
        try {
            AssetManager assetManager = context.getAssets();
            BufferedReader bf = new BufferedReader(new InputStreamReader(
                    assetManager.open(fileName)));
            String line;
            while ((line = bf.readLine()) != null) {
                stringBuilder.append(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return stringBuilder.toString();
    }
    public static String getSubStringEnd(String str,String s) {
        if (TextUtils.isEmpty(str)) {
            return "";
        }
        if(str.endsWith(s)){
            return str.substring(0, str.length()-1);
        }else{
            return str;
        }

    }
    public static void extiWebView(final WebView mWebView){
        if (mWebView != null) {//解决Receiver not registered: android.widget.ZoomButtonsController
            mWebView.getSettings().setBuiltInZoomControls(true);
            mWebView.setVisibility(View.GONE);// 把destroy()延后
            AppContext.mHandler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    mWebView.destroy();
                }
            }, ZOOM_CONTROLS_TIMEOUT + 1000);
        }
    }
}
